package com.zhengtoudai.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;

import com.zhengtoudai.R;
import com.zhengtoudai.view.CustomViewPager;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 用于viewPager包裹Fragment的Activity。实现 四个抽象方法即可。不需设置布局
 */

public abstract class BaseViewPagerActivity extends BaseActivity {
    @Bind(R.id.viewpager)
    CustomViewPager viewpager;
    @Bind(R.id.tab_layout)
    TabLayout mTabLayout;

    private String[] titles;
    private Fragment[] fragments;

    //主题颜色  （选中颜色）
    private int colorStyle = R.color.title_text;
    //未选中颜色
    protected int normalTextColor = R.color.black_3;

    private int background = R.color.white; //背景颜色

    protected abstract String[] getTitles();

    protected abstract Fragment[] getFragments();

    //若需使用其他颜色，复写该方法即可
    protected int ColorStyle() {
        return colorStyle;
    }

    protected abstract void setTitle();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_view_pager);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        setTitle();
    }

    @Override
    protected void initView() {
        titles = getTitles();
        colorStyle = ColorStyle();
        fragments = getFragments();

        mTabLayout.setBackgroundResource(background);
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this,colorStyle));
        mTabLayout.setTabTextColors(ContextCompat.getColor(this,normalTextColor),ContextCompat.getColor(this,colorStyle));

        for (int i = 0; i < titles.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }
        viewpager.setAdapter(new PagerAdapter(fragments,titles,this.getSupportFragmentManager()));
        mTabLayout.setupWithViewPager(viewpager);

    }

    /**
     * 设置ViewPager是否能滑动
    * @param isSlide
    */
    public void setViewpagerSlide(boolean isSlide) {
        viewpager.setScanScroll(isSlide);
    }


    /**
     * 设置ViewPager缓存数量
     * @param pageLimit
     */
    public void setViewpagerSlide(int pageLimit) {
        viewpager.setOffscreenPageLimit(pageLimit);
    }

    /**
     * onCreatePost 之后调用
     * 设置文字
     */
    protected void setTabText(int position, String text) {
        mTabLayout.getTabAt(position).setText(text);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    static class PagerAdapter extends FragmentPagerAdapter {
        private Fragment[] fragments;
        private String[] titles;
        public PagerAdapter(Fragment[] fragments,String[] titles, FragmentManager fm) {
            super(fm);
            this.fragments = fragments;
            this.titles=titles;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
